package com.demo.java.OD451_500.OD468;

import java.util.*;

/**
 * @author bug菌
 * @Source 公众号：猿圈奇妙屋
 * @des： 【手机App防沉迷系统(C&D卷)】问题
 * @url： https://blog.csdn.net/weixin_43970743/article/details/146352614
 */
public class OdMain {
    // 将时间字符串转换为分钟数
    public static int analysisTime(String data) {
        String[] timeComponents = data.split(":");
        int hour = Integer.parseInt(timeComponents[0]);
        int minute = Integer.parseInt(timeComponents[1]);
        return hour * 60 + minute;
    }

    // 查找最大优先级的应用程序，并且按优先级进行排序
    public static List<Map<String, Object>> findMaxPriorityApps(List<Map<String, Object>> apps) {
        List<Map<String, Object>> sortedApps = new ArrayList<>(apps);
        sortedApps.sort(Comparator.comparing((Map<String, Object> x) -> (Integer) x.get("endTime"))
                .thenComparing((Map<String, Object> x) -> -((Integer) x.get("priority"))));

        List<Map<String, Object>> selectedApps = new ArrayList<>();

        // 遍历已排序的应用程序，筛选出没有时间重叠的应用程序
        for (Map<String, Object> app : sortedApps) {
            boolean overlap = selectedApps.stream().anyMatch(r ->
                    ((Integer) app.get("startTime")) <= ((Integer) r.get("endTime"))
                            && ((Integer) app.get("startTime")) <= ((Integer) app.get("endTime")));

            if (!overlap) {
                selectedApps.add(app);
            }
        }

        return selectedApps;
    }

    // 查找在特定时间点正在使用的应用
    public static String findAppAtTime(List<Map<String, Object>> apps, int time) {
        for (Map<String, Object> app : apps) {
            int startTime = (int) app.get("startTime");
            int endTime = (int) app.get("endTime");

            if (startTime <= time && time <= endTime) {
                return (String) app.get("name");
            }
        }

        return "NA"; // 没有应用正在运行
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        // 读取输入
        int n = Integer.parseInt(scanner.nextLine().trim()); // 读取应用程序数量
        List<Map<String, Object>> apps = new ArrayList<>();

        // 读取应用程序信息
        for (int i = 0; i < n; i++) {
            String[] appInfo = scanner.nextLine().split(" ");
            String name = appInfo[0];
            int priority = Integer.parseInt(appInfo[1]);
            int startTime = analysisTime(appInfo[2]);
            int endTime = analysisTime(appInfo[3]);

            Map<String, Object> app = new HashMap<>();
            app.put("name", name);
            app.put("priority", priority);
            app.put("startTime", startTime);
            app.put("endTime", endTime);

            apps.add(app);
        }

        // 读取查询时间
        int time = analysisTime(scanner.nextLine().trim());

        // 找到符合条件的应用程序
        List<Map<String, Object>> selectedApps = findMaxPriorityApps(apps);

        // 输出结果：判断此时正在使用的应用程序
        String ans = findAppAtTime(selectedApps, time);

        System.out.println(ans);
    }
}
